var roleCarrierlab = {

    /** @param {Creep} creep **/
    run: function(creep) {
        var Lab1 = Game.getObjectById('62249314d834b5198b39bef1'); 
        var Lab2 = Game.getObjectById('6224aecd8f0f3e05ec35f87b'); 
        var Lab3 = Game.getObjectById('6224cae31514931a58e48ed9'); 
        //console.log(Lab3.store[RESOURCE_CATALYZED_GHODIUM_ACID]);
        creep.say(creep.memory.building);
        //console.log(creep.store[RESOURCE_GHODIUM_ACID]);
        if(Lab3.store[RESOURCE_CATALYZED_GHODIUM_ACID] > 0){
            creep.memory.building = 3;
            console.log(creep.memory.building, Lab3.pos);
        }
        else if(Lab2.store.getFreeCapacity(RESOURCE_CATALYST) > 200){
            creep.memory.building = 2;
            console.log(creep.memory.building, Lab2.pos);
        }
        else if(Lab1.store.getFreeCapacity(RESOURCE_GHODIUM_ACID) > 200){
            creep.memory.building = 1;
            console.log(creep.memory.building, Lab1.pos);
        }
        // else{
        //     creep.memory.building = 999;
        //     console.log(creep.memory.building);
        // }

        if(creep.memory.building == 1){
            //creep.say('0');
            
            if(creep.store[RESOURCE_GHODIUM_ACID]<=40){
                var targetsgo = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return structure.structureType == STRUCTURE_TERMINAL && structure.store[RESOURCE_GHODIUM_ACID] > 0;
                    }
                });
                if(targetsgo.length > 0) {
                    if(creep.withdraw(targetsgo[0], RESOURCE_GHODIUM_ACID,40) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsgo[0]);
                        creep.say('运来');
                    }
                } 
            }
            else{
                if(creep.transfer(Lab1, RESOURCE_GHODIUM_ACID) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(Lab1);
                        
                }
            }
        }
        else if(creep.memory.building == 2){
            //creep.say('1');
            if(creep.store[RESOURCE_CATALYST]<=40){
                var targetsgo = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return structure.structureType == STRUCTURE_TERMINAL && structure.store[RESOURCE_CATALYST] > 0;
                    }
                });
                if(targetsgo.length > 0) {
                    if(creep.withdraw(targetsgo[0], RESOURCE_CATALYST,40) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsgo[0]);
                        creep.say('运来');
                    }
                } 
            }
            else{
                //creep.say('运qu');
                if(creep.transfer(Lab2, RESOURCE_CATALYST) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(Lab2);
                }
            }
        }
        else if(creep.memory.building == 3){
            if(creep.store[RESOURCE_CATALYZED_GHODIUM_ACID]<=0){
                    if(creep.withdraw(Lab3, RESOURCE_CATALYZED_GHODIUM_ACID) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(Lab3);
                        creep.say('运来');
                    }
            }
            else{
                var targetsgo = creep.room.find(FIND_STRUCTURES, {
                    filter: (structure) => {
                        return structure.structureType == STRUCTURE_TERMINAL;
                    }
                });
                if(targetsgo.length > 0) {
                    if(creep.transfer(targetsgo[0], RESOURCE_CATALYZED_GHODIUM_ACID) == ERR_NOT_IN_RANGE) {
                        creep.moveTo(targetsgo[0]);
                        creep.say('运qu');
                    }
                } 
            }
        }
        else if(creep.memory.building = 999){
            creep.say('xianzhi');
        }
	}
};

module.exports = roleCarrierlab;